

package GUI;

/**
 *
 * @author David Ponce
 * @author Philipp Armbruster
 */

import Interaction.ServerConnection;
import Flightadministration.Flightadministration;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class SeatStructureCreate extends JPanel {

    final Main td;
    private JPanel jp;
    /*Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
    *Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();


    // Konstruktor
    public SeatStructureCreate(final Main td) {
        
        this.td = td;
        showSeatStructureCreate();
    }


    // Methoden
    public void showSeatStructureCreate(){

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************
        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 3;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren


        //Label
        //**********************************************************************

        JLabel headline = new JLabel("<html><h2 color=blue>Sitzstruktur anlegen</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        JLabel label2 = new JLabel("Anzahl Reihen: ");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 1;
        jp.add(label2, constraints);

        final JTextField textfield1 = new JTextField();
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 1;
        jp.add(textfield1, constraints);

        JLabel label3 = new JLabel("Plätze pro Reihe: ");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 2;
        jp.add(label3, constraints);

        final JTextField textfield2 = new JTextField();
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 2;
        jp.add(textfield2, constraints);

        JLabel label4 = new JLabel("Gesamtsitzplatzanzahl: ");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 3;
        jp.add(label4, constraints);

        final JLabel label5 = new JLabel();
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 3;
        jp.add(label5, constraints);



        //Buttons
        //**********************************************************************
        
        JButton button_CreateSeatStructure = new JButton("Sitzplatzstruktur anlegen");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 4;
        jp.add(button_CreateSeatStructure, constraints);


        JButton button_Cancel = new JButton("Zurück zur Übersicht");
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 4;
        jp.add(button_Cancel, constraints);


        //Action Listener
        //**********************************************************************

        textfield1.addCaretListener(new CaretListener() {
                @Override
            public void caretUpdate(CaretEvent e) {
                if (textfield1.getText().equals("")) {
                }
                else if (textfield2.getText().equals("")) {
                    }
                    else {
                        try {
                            int rows = Integer.parseInt(textfield1.getText());
                            int seatsPerRows = Integer.parseInt(textfield2.getText());
                            int sum = rows * seatsPerRows;
                            String gesamt = String.valueOf(sum);
                            label5.setText(gesamt);
                        } catch (NumberFormatException ex) {
                            try{
                                textfield1.setText("");
                                textfield2.setText("");
                                label5.setText("");
                            } catch (IllegalStateException exe) {
                                JOptionPane.showMessageDialog(null, "<html><p style=\"color:red; text-align: center\">" +
                                                                    "Bitte geben Sie nur ganzzahlige Werte ein!</p></html>");
                            }
                        }
                    }
            }   
        });

        
        textfield2.addCaretListener(new CaretListener() {
                @Override
            public void caretUpdate(CaretEvent caretEvent) {
                if (textfield2.getText().equals("")) {
                }
                else if (textfield1.getText().equals("")) {

                    }
                    else {
                        try {
                            int rows2 = Integer.parseInt(textfield1.getText());
                            int seatsPerRows2 = Integer.parseInt(textfield2.getText());
                            int sum2 = rows2 * seatsPerRows2;
                            String gesamt2 = String.valueOf(sum2);
                            label5.setText(gesamt2);
                        } catch (NumberFormatException ex) {
                            try{
                                textfield2.setText("");
                                textfield1.setText("");
                                label5.setText("");
                            } catch (IllegalStateException exe) {
                                JOptionPane.showMessageDialog(null, "<html><p style=\"color:red; text-align: center\">" +
                                                                    "Bitte geben Sie nur ganzzahlige Werte ein!</p></html>");
                            }
                        }
                    }
            }
        });
   

        button_CreateSeatStructure.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                 if ((textfield1.getText().equals("")) ||
                     (textfield2.getText().equals("")))
                {
                    JOptionPane.showMessageDialog(null, "Bitte alle Felder ausfüllen!");
                }
                else {
                    try {

                        String currentSeatsPerRow = textfield1.getText();
                        int currentSeatsPerRowAsInt = Integer.parseInt(currentSeatsPerRow);

                        String currentNrOfRows = textfield2.getText();
                        int currentNrOfRowsAsInt = Integer.parseInt(currentNrOfRows);

                        // Sitzstruktur anlegen
                        admin.createSeat(currentSeatsPerRowAsInt, currentNrOfRowsAsInt);
                            // JTables aktualisieren
                            td.updateSeatStructure();

                        JOptionPane.showMessageDialog(null, "Sitzplatzstruktur angelegt");
                        textfield1.setText("");
                        textfield2.setText("");
                        label5.setText("");

                        ((CardLayout) td.rightp.getLayout()).show(td.rightp, "SeatStructureM");

                    } catch (RemoteException ex) {
                        Logger.getLogger(AircraftCreate.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });


        button_Cancel.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                    ((CardLayout) td.rightp.getLayout()).show(td.rightp,"SeatStructureM");

            }
        });

    }
    
    public JPanel getJp() {
        return jp;
    }

}
